package com.pay;


import com.pay.alipay.config.AliPayConfig;
import com.pay.bean.PayMode;
import com.pay.bean.TradePayInfo;
import com.pay.weixin.WxPayConfig;
import com.tmt.helper.ReflectHelper;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class PayPageLoader {

    private static PayPageLoader instance;

    private Map<String, String> payModeReturnPageMap = new HashMap<>();

    private PayPageLoader() {
    }

    public synchronized static PayPageLoader getInstance() {
        if (instance == null) {
            instance = new PayPageLoader();
        }
        return instance;
    }

    public void addReturnPageForPayMode(String payMode, String returnPage) {
        payModeReturnPageMap.put(payMode, returnPage);
    }


    /**
     * 打开支付页面。
     *
     * @param modelAttributes ;
     * @return
     * @throws Exception
     */
    public String openPayPage(TradePayInfo payInfo, Map<String, Object> modelAttributes) throws Exception {
        return openPayPage(payInfo, null, null, modelAttributes);
    }

    public String openPayPage(TradePayInfo payInfo,
                              AliPayConfig aliPayConfig,
                              WxPayConfig wxPayConfig,
                              Map<String, Object> modelAttributes) throws Exception {

        String html = getPayPageInfo(payInfo.getPayMode(),
                payInfo.getTradeNo(),
                payInfo.getTradeInfo(),
                payInfo.getTotalPrice(),
                payInfo.getTradeDescr(),
                payInfo.getTimeout(),
                aliPayConfig,
                wxPayConfig);

        return getPayPage(payInfo, aliPayConfig, wxPayConfig, html, modelAttributes);
    }

    /**
     * 获取支付页面。
     *
     * @param payMode
     * @param orderCode
     * @param orderInfo
     * @param totalPrice
     * @param description
     * @param timeout
     * @return
     */
    private String getPayPageInfo(String payMode, String orderCode, String orderInfo, BigDecimal totalPrice,
                                  String description, Integer timeout,
                                  AliPayConfig aliPayConfig,
                                  WxPayConfig wxPayConfig) throws Exception {
        PayManager manager = PayManager.getInstance();

        String htmlInfo = "";
        PayMode mode = PayMode.valueOf(payMode);
        switch (mode) {
            case ALI_PAY:
                htmlInfo = manager.aliPay(orderCode, orderInfo, totalPrice, orderCode, description, timeout, aliPayConfig);
                break;
            case WX_PAY:
                htmlInfo = manager.weiXinPay(orderCode, orderInfo, totalPrice, description, timeout, wxPayConfig);
                break;
        }
        return htmlInfo;
    }

    private String getPayPage(TradePayInfo payInfo,
                              AliPayConfig aliPayConfig,
                              WxPayConfig wxPayConfig,
                              String html, Map<String, Object> modelAttributes) {
        PayMode payMode = PayMode.valueOf(payInfo.getPayMode());
        WxPayConfig config = wxPayConfig != null ? wxPayConfig : WxPayConfig.getInstance();
        switch (payMode) {
            case ALI_PAY:
                modelAttributes.put("payForm", html);
                break;
            case WX_PAY:
                modelAttributes.put("urlCode", html);
                modelAttributes.put("tradeNo", payInfo.getTradeNo());

                String queryUrl = String.format(config.getLocalQueryUrl(), payInfo.getTradeNo());
                modelAttributes.put("queryUrl", queryUrl);

                modelAttributes.put("notifyUrl", config.getNotifyUrl());

                Integer timeout = payInfo.getTimeout() != null && payInfo.getTimeout() > 0
                        ? payInfo.getTimeout() : config.getPayTimeout();
                modelAttributes.put("timeout", timeout);
                break;
        }
        return payModeReturnPageMap.get(payInfo.getPayMode());
    }

}
